<?php
session_cache_expire(999999999);
if(!isset($_SESSION)) 
{ 
	session_start(); 
}

include("content_salary/sa_funcs.php");
$mthu=array();
$mthu[0]='<font color="red" ><b>'.$l_vday_chunhat.'</b></font>';
$mthu[1]=$l_vday_thuhai;
$mthu[2]=$l_vday_thuba;
$mthu[3]=$l_vday_thutu;
$mthu[4]=$l_vday_thunam;
$mthu[5]=$l_vday_thusau;
$mthu[6]='<font color="red" ><b>'.$l_vday_thubay.'</b></font>';
$mthu[7]=" ";

if (isset($_SESSION['searchload'])) {
	$searchload = $_SESSION['searchload'];
	$searchFrom = $searchload['datefrom'];
	$searchTo = $searchload['dateto'];
	$searchName = $searchload['employeename'];
}
$_SESSION['thongbao_chamcong']="";
?>
<style>
<!--
* { margin:0;}
-->
</style>
<script type="text/javascript" src="js/jquery-1.8.2.js"></script>
<link rel="stylesheet" type="text/css" href="css/jquery-ui.css" />
<script type="text/javascript" src="js/jquery-ui.js"></script>

<link rel="stylesheet"
	type="text/css" href="admin/css/jsDatePick_ltr.min.css" />

<script
	type="text/javascript" src="admin/css/jsDatePick.min.1.3.js"></script>

<script type="text/javascript">
$(document).ready(function(){
	$(".hang").mouseover(function(){	
		$(this).addClass("nenhang");	
	});
	
	$(".hang").mouseout(function(){	
		$(this).removeClass("nenhang");	
	});
});

window.onload = function(){

new JsDatePick({

useMode:2,

target:"datefrom",
dateFormat:"%Y-%m-%d"

});

new JsDatePick({

	useMode:2,

	target:"dateto",
	dateFormat:"%Y-%m-%d"

	});
};

	
</script>
<script type="text/javascript">
$(function() {
	var dateupdate = $( "#dateupdate" ),
	inupdate = $( "#inupdate" ),
	outupdate = $( "#outupdate" ),
	allFields = $( [] ).add( dateupdate ).add( inupdate ).add( outupdate );
		
	function checkLength( o, n, min, max ) {
		if ( o.val().length > max || o.val().length < min ) {
			o.addClass( "ui-state-error" );			
			return false;
		} else {
			return true;
		}
	}

	function checkRegexp( o, regexp ) {
		if ( !( regexp.test( o.val() ) ) ) {
			o.addClass( "ui-state-error" );
			return false;
		} else {
			return true;
		}
	}

	$("#dialog-update").dialog({
		autoOpen : false,
		modal : true,
		height:350,
		width:500,
		buttons: {
			"<?=$l_update?>": function() {
				var bValid = true;
				allFields.removeClass( "ui-state-error" );

				bValid = bValid && checkLength( dateupdate, "dateupdate", 1,10 );
				
				bValid = bValid && checkLength( inupdate , "inupdate", 3, 5 );//
				bValid = bValid && checkRegexp( inupdate, /^(\d{1,2}\:\d{1,2})$/ );
				
				bValid = bValid && checkLength( outupdate, "outupdate", 3, 5 );
				bValid = bValid && checkRegexp( outupdate, /^(\d{1,2}\:\d{1,2})$/ );
				var ind = document.getElementById("inupdate").value;
				var outd = document.getElementById("outupdate").value;
				if ( bValid || (ind==0 && outd==0) ) {
					hoi= confirm("<?php echo $l_areyousure;?>");
					if (hoi==true) {
						$('form#formupdate').submit();
						$( this ).dialog( "close" );
					}else{
						allFields.removeClass( "ui-state-error" );
					}
				}
			},
			'<?=$l_cancel?>': function() {
				allFields.removeClass( "ui-state-error" );
				$( this ).dialog( "close" );
			}
		}
	});

	new JsDatePick({

		useMode:2,

		target:"dateupdate",
		dateFormat:"%Y-%m-%d"

	});
	
	function showDialog(){
		$("#dialog-update").dialog('open');
	}
	
});
</script>

<script type="text/javascript">
var myinterval;
function updateCheckTime(ele){
	parentTR = $(ele).parent().parent();
	var idupdate = $(parentTR).find('.idupdate').val();
	var dateupdate = $(parentTR).find('.dateupdate').val();
	var inupdate = $(parentTR).find('.inupdate').val();
	var outupdate = $(parentTR).find('.outupdate').val();
	var noteupdate = $(parentTR).find('.noteupdate').val();
	$( "#idupdate" ).val(idupdate);
	$( "#dateupdate" ).val(dateupdate);
	$( "#inupdate" ).val(inupdate);
	$( "#outupdate" ).val(outupdate);
	$( "#noteupdate" ).val(noteupdate);
	myinterval = setInterval(function(){myTimer()},1);	
	$("#dialog-update").dialog('open');
	
}
function myTimer(){
	$( "#dateupdate" ).parent().find('div.JsDatePickBox').hide();
	clearInterval(myinterval);
}

function loadData(){
	var datefrom = $('input#datefrom').val();
	var dateto = $('input#dateto').val();
	var employeename = $('input#employeetxt').val();
	var l_list_emplty = "<?=$l_list_emplty?>";

	/*$.ajax({url:"content_salary/ajaxloadworkingspreadsheet.php",data : {datefrom : datefrom, dateto:dateto,employeename:employeename, l_list_emplty:l_list_emplty } , success:function(result){
		$('table#checktimes tr:not(:first)').remove();
		$('table#checktimes').append(result);

		$(".hang").mouseover(function(){	
			$(this).addClass("nenhang");	
		});
		
		$(".hang").mouseout(function(){	
			$(this).removeClass("nenhang");	
		});
	}});*/
	var body = document.getElementById('checktime');
	body.setAttribute('style','opacity:.5; position:relative;cursor: wait;  ');
	
	var d=document.createElement('div');
	d.setAttribute('style','display: block;visibility: visible; position: absolute;  z-index: 999; top: 0px; left: 0px; width: 105%; height: 105%; text-align: center;  filter: alpha(opacity=5); opacity: 0.5; ');
	
	var image=document.createElement('img');
	image.setAttribute('src','images/loading1.gif');
	image.setAttribute('style','position:absolute; left:0px; top:0px; right:0; bottom:0; margin:auto');
	
	d.appendChild(image);
	
    body.appendChild(d);
	if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
	}else{// code for IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange=function(){
										  if (xmlhttp.readyState==4 && xmlhttp.status==200){
										    	document.getElementById("checktime").innerHTML=xmlhttp.responseText;
										    	document.getElementById('checktime').setAttribute('style','opacity:1; position:relative;');
										    	$(".hang").mouseover(function(){	
													$(this).addClass("nenhang");	
												});
												
												$(".hang").mouseout(function(){	
													$(this).removeClass("nenhang");	
												});
										  }
									  }
	xmlhttp.open("GET","content_salary/ajaxloadworkingspreadsheet.php?datefrom="+datefrom+"&dateto="+dateto+"&employeename="+encodeURIComponent(employeename)+"&l_list_emplty="+l_list_emplty,true);
	xmlhttp.send();
	
}

function showEmployee(){
	if($('div#content_menu_expand').is(':hidden')){
		$('div#content_menu_expand').show();
	}else{
		$('div#content_menu_expand').hide();
	}
}

function chooseEmployee(gt){
	document.getElementById("employeetxt").value=gt;
	$('div#content_menu_expand').hide();
}
</script>
<script type="text/javascript">
function checkDelete(){
	if ($('table#checktimes tr').size()<2){
		return false;
	}else if($('input[title="checkCT"]:checked').size()==0){
		alert('<?=$l_nochoose?>');
		return false;
	}
	hoi= confirm("<?php echo $l_delete_only_not_saved;?>"+"\n\n"+"<?php echo $l_areyousure;?>");
	if (hoi==true) {
		return true;
	}
	return false;
}
function checkAll(ele){
	if($(ele).is(':checked')==true){
		$('table#checktimes tr:not(:first) td:first-child input:checkbox').attr('checked',true);
	}else{
		$('table#checktimes tr:not(:first) td:first-child input:checkbox').attr('checked',false);
	}
}

function sendmail(dfrom,dto,dname){
	if(dname==""){
		dname="all";
	}
	hoi= confirm("<?php echo $l_areyousure;?>");
	if (hoi==true) {
		var body = document.getElementById('checktime');
		body.setAttribute('style','opacity:.5; position:relative;cursor: wait;  ');
		
		var d=document.createElement('div');
		d.setAttribute('style','display: block;visibility: visible; position: absolute;  z-index: 999; top: 0px; left: 0px; width: 105%; height: 105%; text-align: center;  filter: alpha(opacity=5); opacity: 0.5; ');
		d.setAttribute('id','hanhanhan');
		var image=document.createElement('img');
		image.setAttribute('src','images/loading1.gif');
		image.setAttribute('style','position:absolute; left:0px; top:0px; right:0; bottom:0; margin:auto');
		
		d.appendChild(image);
		
	    body.appendChild(d);	
		if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
	 		xmlhttp=new XMLHttpRequest();
	 	}else{// code for IE6, IE5
	 		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	 	}
	 	xmlhttp.onreadystatechange=function(){
			 	if (xmlhttp.readyState==4 && xmlhttp.status==200){
			 		document.getElementById("showtb").innerHTML=xmlhttp.responseText;
			 		var tb=document.getElementById("viewthongbao").value;
			 		if(tb!=""){
			 			alert(tb);
			 		}else{
						alert("null");
			 		}
			 		document.getElementById('checktime').setAttribute('style','opacity:1; position:relative;');
			 		document.getElementById('hanhanhan').style.display='none';
			 		document.getElementById('checktime').parentNode.removeChild('hanhanhan');
			 		
			 	}
		 	}
	 	xmlhttp.open("POST","ajax/salary/sa_sendmail_chamcong.php?dfrom="+dfrom+"&dto="+dto+"&dname="+dname,true);
	 	xmlhttp.send();
	}
}
</script>
<script type="text/javascript">
$(function(){
	$(document).click(function() {
		$("div#content_menu_expand").hide();
	});
	$('#content_menu_expand').click(function(e) {
		e.stopPropagation();
	});
	$('#menu_expand').click(function() {
		if ($('#content_menu_expand').is(":hidden")) {
			$('#content_menu_expand').show();
		}
		else {
			$('#content_menu_expand').hide();
		}
		return false;
	});
	
});
function enterTable(e){
    var key = window.event ? e.keyCode : e.which;
    if(key==13)
    	loadData();
}
</script>
<style type="text/css">
#content_menu_expand {
	position: absolute;
	right: -5px;
	top: 20px;
	z-index: 999;
	background-color: #ffffff;
	-moz-border-radius: 7px;
	-webkit-border-radius: 7px;
	color: #000000;
	width: 300px;
	max-height: 180px;
	overflow: auto;
}
</style>
<link	href="css/style.css" rel="stylesheet" type="text/css" />
<div id="contains">
	<div class="title" style="padding-top: 5px;font-weight: bold;"><?php echo $l_accountant;?> &rarr; <?php echo $l_ml_tinh_luong;?> &rarr; <?php echo $l_working_spreadsheet;?></div>
	<div class="content_code">
		<div class="approval-right" style="padding-right: 10px">
			<div
				style="width: 820px; border: 1px solid silver; padding: 10px 0; margin-bottom: 10px; background: white">
				<table width="820px" style="padding: 0 10px">
					<tr>
						<td style="width: 120px"><?php echo $l_from;?></td>
						<td style="width: 120px"><?php echo $l_to;?></td>
						<td style="width: 320px"><?=$l_employee_name?></td>
						<td style="width: 130px;"></td>
						<td style="width: 130px;"></td>
					</tr>
					<tr>
						<td style="width: 120px"><input type="text" style="height: 19px;"
							id="datefrom" name="datefrom" readonly="readonly" value="<?=$searchFrom?>"/>  
						</td>
						<td style="width: 120px"><input type="text" id="dateto"
							style="height: 19px;" name="dateto" readonly="readonly" value="<?=$searchTo?>"/>
						</td>

						<td style="width: 350px">
							<div style="position: relative;">
								<div style="float: left;">
									<input type="text" size="30" id="employeetxt" value="<?=$searchName?>"  onkeypress="enterTable(event);"
										style="height: 22px; width: 300px; border: 1px solid #c4c0c9; font-size: 12px;"
										autocomplete="off" />
									<div class="suggestionsBox" id="suggestions"
										style="display: none;">
										<img src="images/upArrow.png"
											style="position: relative; top: -12px; left: 30px;"
											alt="upArrow" />
										<div class="suggestionList" id="autoSuggestionsList">&nbsp;</div>
									</div>
								</div>
								<div
									style="float: left; position: relative; margin-top: 4px; margin-left: -21px; z-index: 999">
									<img alt="" src="images/arrow-down.png" id="menu_expand"
										>
									<div id="content_menu_expand" class="passul"
										style="display: none;">
										<ul>
										<?php
										$sql="select IDEmployee, UserName, FullName from employee where  state = 1";
										$result = mysql_query($sql);
										if($result)
										{
											$i=0;
											while($row=mysql_fetch_array($result))
											{
												?>
											<li onmouseover="this.style.cursor='pointer'"
												onmouseout="this.style.cursor='default'"
												onclick="chooseEmployee('<?=$row['FullName']?>')"
												<?php
												if($i%2!=0){chooseEmployee
												?>
												class="bgkhac" <?php
											}
											?>><?php echo $row['FullName'];?></li>
											<?php
											$i++;
											}
										}
										?>
										</ul>
									</div>
								</div>
							</div>
						</td>
						<td style="width: 130px;" align="right">
						<input type="button" style="width:100px;" name="Search" class="btnupbg btnupload"
							value="<?=$l_search?>" onclick="loadData()" />
						</td>
						<td style="width: 130px;"></td>

					</tr>
				</table>
			</div>
			<form id="formchecktime" method="post" onsubmit="return checkDelete();"
				action="content_salary/deleteworkingspreadsheet.php">
				<div>
					<input class="buttonProject" style="margin: 5px 0;" value="<?=$l_delete?>"
						type="submit"> 
					<a href="content_salary/ajax_exportchecktime.php?">
						<input type="button" style="width: 100px;" class="buttonProject" value="<?=$l_export_excel?>" /> 
					</a>
				</div>
				<div id="checktime">
				<table style="width: 100%;" class="rf-dt" id="checktimes">
					<tr>
						<td class="rf-dt-shdr-c" width="2%">No.</td>
						<td class="rf-dt-shdr-c" width="10px"><input onclick="checkAll(this)" type="checkbox" /></td>
						<td class="rf-dt-shdr-c" width="15%"><?=$l_employee_name?></td>
						<td class="rf-dt-shdr-c" width="5%"><?=$l_code?></td>
						<td class="rf-dt-shdr-c" width="5%"><?=$l_thu?></td>
						<td class="rf-dt-shdr-c" width="10%"><?=$l_date?></td>
						<td class="rf-dt-shdr-c" width="5%"><?=$l_timecheckin?></td>
						<td class="rf-dt-shdr-c" width="5%"><?=$l_timecheckout?></td>
						<td class="rf-dt-shdr-c" width="5%"><?=$l_late?></td>
						<td class="rf-dt-shdr-c" width="5%"><?=$l_comebackearly?></td>
						<td class="rf-dt-shdr-c" width="5%"><?=$l_half_dayoff?></td>
						<td class="rf-dt-shdr-c" width="5%"><?=$l_all_dayoff?></td>
						<td class="rf-dt-shdr-c" width="5%"><?=$l_overtime?></td>
						<td class="rf-dt-shdr-c" width="10%"><?=$l_note?></td>
						<td class="rf-dt-shdr-c" width="8%"><?=$l_status?></td>
						<td class="rf-dt-shdr-c" width="13%"><?=$l_update_information?></td>
					</tr>
					<?php
					if (isset($_SESSION['loadCheckTimes']) && is_array($_SESSION['loadCheckTimes'])){
						include 'CheckTime.php';
						include '../operations/connection.php';
						$flagEmpty = true;	
						$i=1;
						$temp="";
						foreach ($_SESSION['loadCheckTimes'] as $key =>  $object){
							
							$CT = unserialize (serialize ($object));
							$thu=getWday($CT->date);
							$flagEmpty = false;
							if($thu==6 || $thu==0){
								?>
								<tr style="background:#ccffcc;">
								<?php 
							}else{
							?>
								<tr >
							<?php
							}
							if($temp!=$CT->employeeID){
								$temp=$CT->employeeID;
								$i=1;
							}else{
								$i=$i+1;
							}
							
							echo '<td class="rf-dt-c" style="text-align:center;">';
							echo $i;
							echo '</td>';
							
							if($CT->is_status==0){
								echo '<td class="rf-dt-c" style="align:center">';
								echo '<input title="checkCT" name="ctime['.$key.']" type="checkbox"/><input type="hidden" class="idupdate" name="vtime['.$key.']" value="'.$CT->id.'"/>';
								echo '</td>';
	
								echo '<td class="rf-dt-c" style="color:blue"><span onmouseover="this.style.cursor=\'pointer\'" onmouseout="this.style.cursor=\'default\'" onclick="updateCheckTime(this)">';
								echo $CT->employeeName;
								echo '</span></td>';
	
								echo '<td class="rf-dt-c" style="color:blue;text-align:center;"><span onmouseover="this.style.cursor=\'pointer\'" onmouseout="this.style.cursor=\'default\'" onclick="updateCheckTime(this)">';
								echo $CT->employeeID;
								echo '</span></td>';
								
								echo '<td class="rf-dt-c" style="color:blue;text-align:center;"><span onmouseover="this.style.cursor=\'pointer\'" onmouseout="this.style.cursor=\'default\'" onclick="updateCheckTime(this)">';
								echo $mthu[$thu];
								echo '</span></td>';
	
								echo '<td class="rf-dt-c" style="color:blue;text-align:center;"><span onmouseover="this.style.cursor=\'pointer\'" onmouseout="this.style.cursor=\'default\'" onclick="updateCheckTime(this)">';
								echo $CT->date;
								echo '</span><input type="hidden" class="dateupdate" value="'.$CT->date.'" />';
								echo '</td>';
							}else{
								echo '<td class="rf-dt-c" style="align:center">';
								//echo '<input title="checkCT" name="ctime['.$key.']" type="checkbox"/><input type="hidden" class="idupdate" name="vtime['.$key.']" value="'.$CT->id.'"/>';
								echo '</td>';
	
								echo '<td class="rf-dt-c" >';
								echo $CT->employeeName;
								echo '</td>';
	
								echo '<td class="rf-dt-c" style="text-align:center;">';
								echo $CT->employeeID;
								echo '</td>';
								
								echo '<td class="rf-dt-c" style="text-align:center;">';
								echo $mthu[$thu];
								echo '</td>';
	
								echo '<td class="rf-dt-c" style="text-align:center;">';
								echo $CT->date;
								echo '<input type="hidden" class="dateupdate" value="'.$CT->date.'" />';
								echo '</td>';
							}

							echo '<td class="rf-dt-c" style="text-align:center;">';
							if ($CT->inTime != '0') echo $CT->inTime;
							echo '<input type="hidden" class="inupdate" value="'.$CT->inTime.'" />';
							echo '</td>';

							echo '<td class="rf-dt-c" style="text-align:center;">';
							if ($CT->outTime != '0') echo $CT->outTime;
							echo '<input type="hidden" class="outupdate" value="'.$CT->outTime.'" />';
							echo '</td>';

							if ($CT->late != '0'){
								echo '<td class="rf-dt-c" style="font-weight:bold;background:orange;text-align:center;">X</td>';
							}else{
								echo '<td class="rf-dt-c"></td>';
							}
							

							if ($CT->early != '0'){
								echo '<td class="rf-dt-c" style="font-weight:bold;background:yellow;text-align:center;">X</td>';
							}else{
								echo '<td class="rf-dt-c"></td>';
							}

							if ($CT->halfDayOff != '0') {
								echo '<td class="rf-dt-c" style="font-weight:bold;background:pink;text-align:center;">X</td>';
							}else{
								echo '<td class="rf-dt-c"></td>';
							}

							if ($CT->aDayOff != '0'){
								echo '<td class="rf-dt-c" style="font-weight:bold;background:red;text-align:center;">X</td>';
							}else{
								echo '<td class="rf-dt-c"></td>';
							}

							if ($CT->overtime != '0') {
								echo '<td class="rf-dt-c" style="font-weight:bold;background:green;text-align:center;">';
								for ($i=0;$i<$CT->overtime;$i++){
									echo 'X';
								}
								echo '</td>';
							}else{
								echo '<td class="rf-dt-c"></td>';
							}
							
							echo '<td class="rf-dt-c" style="text-align:center;">';
							echo $CT->note;
							echo '<input type="hidden" class="noteupdate" value="'.$CT->note.'" />';
							echo '</td>';
							
							?>
							<td class="rf-dt-c" style="text-align:center;">
								<?php 
									if($CT->is_status==0){
										echo $l_co_the_sua;
									}else if($CT->is_status==2){
										echo $l_da_tinh_luong;
									}
								?>
							</td>
							<?php 
							?>
							<td class="rf-dt-c" style="text-align:center;">
								<?php echo $CT->update_user."<br>".$CT->update_time;?>
							</td>
							</tr>
							<?php 
						}
						if ($flagEmpty==true) {
							echo '<tr>';
							echo '<td class="rf-dt-c" colspan="12">';
							echo $l_list_emplty;
							echo '</tr>';
							echo '</tr>';
						}
					}?>
				</table>
				</div>
			</form>
			<br />
		
			<div id="dialog-update" title="<?=$l_update?>">
				<form id="formupdate" method="post"
				action="content_salary/updateworkingspreadsheet.php">
					<br/>
					<input type="hidden" name='idupdate' id='idupdate'/>
					<div style="padding-top:5px"><label><font color="red">(*)</font><?=$l_date?></label></div>
					<div><input type="text" class="text ui-widget-content ui-corner-all" style="height: 24px;width: 100%" name="dateupdate" id="dateupdate" readonly="readonly"/></div>
					
					<div style="padding-top:5px"><label><font color="red">(*)</font><?=$l_timecheckin?></label></div>
					<div><input type="text" class="text ui-widget-content ui-corner-all" style="height: 24px;width: 100%" name="inupdate" id="inupdate" /></div>
					
					<div style="padding-top:5px"><label><font color="red">(*)</font><?=$l_timecheckout?></label></div>
					<div><input type="text" class="text ui-widget-content ui-corner-all" style="height: 24px;width: 100%" name="outupdate" id="outupdate" /></div>
					
					<div style="padding-top:5px"><label ><?=$l_note?></label></div>
					<div><input type="text" class="text ui-widget-content ui-corner-all" style="height: 24px;width: 100%" name="noteupdate" id="noteupdate" /></div>
					<div><br><span><?php echo $l_vday_tb;?></span></div>
				</form>
		</div>
	</div>

</div>
</div>
